﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class AddStudentForm : Form
    {
        public AddStudentForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string stuName = txtStuName.Text.Trim();
            int classId = (int)cboClasses.SelectedValue;
            string sex = rbtMale.Checked ? rbtMale.Text.Trim() : rbtFemale.Text.Trim();
            string phone = txtPhone.Text.Trim();
            if (string.IsNullOrEmpty(stuName))
            {
                MessageBox.Show("学生姓名不能为空！", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(phone))
            {
                MessageBox.Show("电话不能为空！", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string sql = "select count(1) from StudentInfo where StuName=@StuName and Phone=@Phone";
            SqlParameter[] paras =
            {
                new SqlParameter("@StuName", stuName),
                new SqlParameter("@Phone", phone)
            };
            object obj = SqlHelper.ExecuteScalar(sql, paras);
            if (obj == null || obj == DBNull.Value || ((int)obj) == 0)
            {
                string sqlAdd = "insert into StudentInfo(StuName,ClassId,Sex,Phone) values(@StuName,@ClassId,@Sex,@Phone)";
                SqlParameter[] parasAdd =
                {
                    new SqlParameter("@StuName", stuName),
                    new SqlParameter("@ClassId", classId),
                    new SqlParameter("@Sex", sex),
                    new SqlParameter("@Phone", phone)
                };
                int count = SqlHelper.ExecuteNonQuery(sqlAdd, parasAdd);
                if (count > 0)
                {
                    MessageBox.Show($"学生：{stuName}添加成功!", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    MessageBox.Show("该学生添加失败，请检查！", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                MessageBox.Show("该学生已存在！", "添加学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void AddStudentForm_Load(object sender, EventArgs e)
        {
            InitClass();
            rbtMale.Checked = true;
        }

        private void InitClass()
        {
            string sql = "select ClassId,ClassName,GradeName from ClassInfo c,GradeInfo g where c.GradeId=g.GradeId";
            DataTable dataTable = SqlHelper.GetDataTable(sql);
            if (dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    string ClassName = row["ClassName"].ToString();
                    string gradeName = row["GradeName"].ToString();
                    row["ClassName"] = ClassName + "--" + gradeName;
                }
            }
            cboClasses.DataSource = dataTable;
            cboClasses.DisplayMember = "ClassName";
            cboClasses.ValueMember = "ClassId";
            cboClasses.SelectedIndex = 0;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
